home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / cleanup-info < prev    next >
Encoding:
Text File  |  2007-03-06  |  4.8 KB  |  182 lines

  1. #!/usr/bin/perl --
  2. #
  3. #   Clean up the mess that bogus install-info may have done :
  4. #
  5. #    - gather all sections with the same heading into a single one.
  6. #    Tries to be smart about cases and trailing colon/spaces.
  7. #
  8. #   Other clean ups :
  9. #
  10. #    - remove empty sections,
  11. #    - squeeze blank lines (in entries part only).
  12. #
  13. #   Order of sections is preserved (the first encountered section
  14. # counts).
  15. #
  16. #   Order of entries within a section is preserved.
  17. #
  18. # BUGS:
  19. #
  20. #   Probably many : I just recently learned Perl for this program
  21. # using the man pages.  Hopefully this is a short enough program to
  22. # debug.
  23.  
  24. # don't put that in for production.
  25. # use strict;
  26.  
  27. my $dpkglibdir="/usr/lib/dpkg"; # This line modified by Makefile
  28. push(@INC,$dpkglibdir);
  29. require 'dpkg-gettext.pl';
  30. textdomain("dpkg");
  31.  
  32. ($0) = $0 =~ m:.*/(.+):;
  33.  
  34. my $version="1.13.24"; # This line modified by Makefile
  35. sub version {
  36.     printf _g("Debian %s version %s.\n"), $0, $version;
  37.  
  38.     printf _g("
  39. Copyright (C) 1996 Kim-Minh Kaplan.");
  40.  
  41.     printf _g("
  42. This is free software; see the GNU General Public Licence version 2 or
  43. later for copying conditions. There is NO warranty.
  44. ");
  45. }
  46.  
  47. sub usage {
  48.     printf _g(
  49. "Usage: %s [<option> ...] [--] [<dirname>]
  50.  
  51. Options:
  52.   --unsafe     set some additional possibly useful options.
  53.                warning: this option may garble an otherwise correct file.
  54.   --help       show this help message.
  55.   --version    show the version.
  56. "), $0;
  57. }
  58.  
  59. my $infodir = '/usr/info';
  60. my $unsafe = 0;
  61. $0 =~ m|[^/]+$|;
  62. my $name= $&;
  63.  
  64. sub ulquit {
  65.     unlink "$infodir/dir.lock"
  66.     or warn sprintf(_g("%s: warning - unable to unlock %s: %s"),
  67.                     $name, "$infodir/dir", $!)."\n";
  68.     die "$name: $_[0]";
  69. }
  70.  
  71. while (scalar @ARGV > 0 && $ARGV[0] =~ /^--/) {
  72.     $_ = shift;
  73.     /^--$/ and last;
  74.     /^--version$/ and do {
  75.     version;
  76.     exit 0;
  77.     };
  78.     /^--help$/ and do {
  79.     usage;
  80.     exit 0;
  81.     };
  82.     /^--unsafe$/ and do {
  83.     $unsafe=1;
  84.     next;
  85.     };
  86.     printf STDERR _g("%s: unknown option \`%s'")."\n", $name, $_;
  87.     usage;
  88.     exit 1;
  89. }
  90.  
  91. if (scalar @ARGV > 0) {
  92.     $infodir = shift;
  93.     if (scalar @ARGV > 0) {
  94.     printf STDERR _g("%s: too many arguments")."\n", $name;
  95.     usage;
  96.     exit 1;
  97.     }
  98. }
  99.  
  100. if (!link "$infodir/dir", "$infodir/dir.lock") {
  101.     die sprintf(_g("%s: failed to lock dir for editing! %s"),
  102.                 $name, $!)."\n".
  103.         ($! =~ /exist/i ? sprintf(_g("try deleting %s"),
  104.                                   "$infodir/dir.lock")."\n" : '');
  105. }
  106. open OLD, "$infodir/dir"
  107.     or ulquit sprintf(_g("unable to open %s: %s"), "$infodir/dir", $!)."\n";
  108. open OUT, ">$infodir/dir.new"
  109.     or ulquit sprintf(_g("unable to create %s: %s"), "$infodir/dir.new", $!)."\n";
  110.  
  111. my (%sections, @section_list, $lastline);
  112. my $section="Miscellaneous";    # default section
  113. my $section_canonic="miscellaneous";
  114. my $waitfor = $unsafe ? '^\*.*:' : '^\*\s*Menu';
  115.  
  116. while (<OLD>) {                # dump the non entries part
  117.     last if (/$waitfor/oi);
  118.     if (defined $lastline) {
  119.     print OUT $lastline
  120.         or ulquit sprintf(_g("unable to write %s: %s"),
  121.                  "$infodir/dir.new", $!)."\n";
  122.     }
  123.     $lastline = $_;
  124. };
  125.  
  126. if (/^\*\s*Menu\s*:?/i) {
  127.     print OUT $lastline if defined $lastline;
  128.     print OUT $_;
  129. } else {
  130.     print OUT "* Menu:\n";
  131.     if (defined $lastline) {
  132.     $lastline =~ s/\s*$//;
  133.     if ($lastline =~ /^([^\*\s].*)/) { # there was a section title
  134.         $section = $1;
  135.         $lastline =~ s/\s*:$//;
  136.         $section_canonic = lc $lastline;
  137.     }
  138.     }
  139.     push @section_list, $section_canonic;
  140.     s/\s*$//;
  141.     $sections{$section_canonic} = "\n$section\n$_\n";
  142. }
  143.  
  144. foreach (<OLD>) {        # collect sections
  145.     next if (/^\s*$/ or $unsafe and /^\*\s*Menu/oi);
  146.     s/\s*$//;
  147.     if (/^([^\*\s].*)/) {        # change of section
  148.     $section = $1;
  149.     s/\s*:$//;
  150.     $section_canonic = lc $_;
  151.     } else {            # add to section
  152.     if (! exists $sections{$section_canonic}) { # create section header
  153.         push @section_list, $section_canonic;
  154.         $sections{$section_canonic} = "\n$section\n";
  155.     }
  156.     $sections{$section_canonic} .= "$_\n";
  157.     }
  158. }
  159.  
  160. eof OLD or ulquit sprintf(_g("unable to read %s: %s"), "$infodir/dir", $!)."\n";
  161. close OLD or ulquit sprintf(_g("unable to close %s after read: %s"),
  162.                                "$infodir/dir", $!)."\n";
  163.  
  164. print OUT @sections{@section_list};
  165. close OUT or ulquit sprintf(_g("unable to close %s: %s"),
  166.                                "$infodir/dir.new", $!)."\n";
  167.  
  168. # install clean version
  169. unlink "$infodir/dir.old";
  170. link "$infodir/dir", "$infodir/dir.old"
  171.     or ulquit sprintf(_g("unable to backup old %s, giving up: %s"),
  172.                          "$infodir/dir", $!)."\n";
  173. rename "$infodir/dir.new", "$infodir/dir"
  174.     or ulquit sprintf(_g("failed to install %s; it will be left as %s: %s"),
  175.                          "$infodir/dir", "$infodir/dir.new", $!)."\n";
  176.  
  177. unlink "$infodir/dir.lock"
  178.     or die sprintf(_g("%s: unable to unlock %s: %s"),
  179.                    $name, "$infodir/dir", $!)."\n";
  180.  
  181. exit 0;
  182.